package com.yanqu.road.dao.impl.activity.huashigang;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangMonsterData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class HuaShiGangMonsterDataDaoImpl extends TempDataObjectDao<HuaShiGangMonsterData> {
    public static String getHuaShiGangMonsterDataSql = "select * from t_c_huashigang_monster where activityId=?";

    public Map<String, List<HuaShiGangMonsterData>> getHuaShiGangMonsterData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, List<HuaShiGangMonsterData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaShiGangMonsterDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangMonsterData huaShiGangMonsterData = parse(rs);
                if (!map.containsKey(huaShiGangMonsterData.getPointKey())) {
                    map.put(huaShiGangMonsterData.getPointKey(), new ArrayList<>());
                }
                map.get(huaShiGangMonsterData.getPointKey()).add(huaShiGangMonsterData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHuaShiGangMonsterDataSql = "update t_c_huashigang_monster set allBlood=?,nowBlood=?,killPlayer=?,killUserId=? where activityId=? and pointKey=? and monsterId=?";

    @Override
    protected boolean updateData(HuaShiGangMonsterData huaShiGangMonsterData) {
        huaShiGangMonsterData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangMonsterData.getAllBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangMonsterData.getNowBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangMonsterData.getKillPlayer())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangMonsterData.getKillUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangMonsterData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangMonsterData.getPointKey()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangMonsterData.getMonsterId()));
        if (getDb().executeNoneQuery(updateHuaShiGangMonsterDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangMonsterDataSql = "insert into t_c_huashigang_monster(activityId,pointKey,monsterId,allBlood,nowBlood,killPlayer,killUserId) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HuaShiGangMonsterData huaShiGangMonsterData) {
        huaShiGangMonsterData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangMonsterData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangMonsterData.getPointKey()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangMonsterData.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangMonsterData.getAllBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangMonsterData.getNowBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangMonsterData.getKillPlayer())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangMonsterData.getKillUserId()));
        if (getDb().executeNoneQuery(insertHuaShiGangMonsterDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangMonsterData parseData(ResultSet rs) throws SQLException {
        HuaShiGangMonsterData huaShiGangMonsterData = new HuaShiGangMonsterData();
        huaShiGangMonsterData.setActivityId(rs.getInt("activityId"));
        huaShiGangMonsterData.setPointKey(rs.getString("pointKey"));
        huaShiGangMonsterData.setMonsterId(rs.getInt("monsterId"));
        huaShiGangMonsterData.setAllBlood(rs.getLong("allBlood"));
        huaShiGangMonsterData.setNowBlood(rs.getLong("nowBlood"));
        huaShiGangMonsterData.setKillPlayer(JSONObject.parseObject(rs.getString("killPlayer"), UserBaseInfo.class));
        huaShiGangMonsterData.setKillUserId(rs.getLong("killUserId"));

        return huaShiGangMonsterData;
    }
}
